//
//  MyView.m
//  ceshi
//
//  Created by liutaifeng on 16/5/18.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "MyView.h"
#import "Common.h"
#import <BmobSDK/Bmob.h>
#import "UIImageView+WebCache.h"

@interface MyView  ()
<
 UITableViewDataSource,
 UITableViewDelegate,
 UIAlertViewDelegate
>
//头视图
@property (nonatomic, strong) UIView *topView;
//头像
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *setArray;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UIButton *exitButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIAlertView *alert;
@property (nonatomic, strong) UITextField *text;
@property (nonatomic, strong) InformationTableViewCell *cell;
@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIView *headLine;
@end

@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    
    
    
    
//    //弹出框
    self.alert = [[UIAlertView alloc] initWithTitle:@"请输入昵称" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    _alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    _alert.delegate = self;
    self.text = [_alert textFieldAtIndex:0];
    //数组
    self.titleArray = [NSMutableArray arrayWithObjects:@"昵称", @"手机号", @"邮箱", @"", @"手势密码锁", @"批量删除", @"", @"入门帮助", @"关于我们", nil];
//    self.setArray = [NSMutableArray arrayWithObjects:@"用户名", @"手机号", @"邮箱", nil];
    
    [self getToBmob];
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT / 4 + 50)];
    
    //头像
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH / 4, MAIN_WIDTH / 4)];
    _headImageView.backgroundColor = [UIColor whiteColor];
    [_topView addSubview:_headImageView];
    _headImageView.center = CGPointMake(_topView.center.x, _topView.center.y + 25);
    _headImageView.layer.borderWidth = 1;
    _headImageView.layer.cornerRadius = MAIN_WIDTH / 8;
    
    _headImageView.layer.masksToBounds = YES;
    self.headImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(clickHeadImageView)];
    
    [self.headImageView addGestureRecognizer:tap];
    
    BmobUser *bUser = [BmobUser getCurrentObject];
    if (bUser) {
        //进行操作
        BmobFile *file = (BmobFile *)[bUser objectForKey:@"image"];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:file.url]];
        
    }else{
        //对象为空时，可打开用户注册界面
    }
    
    //线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 4 + 50 - 1, MAIN_WIDTH, 1)];
    [_topView addSubview:_lineView];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    
    
    //tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT) style:UITableViewStylePlain];
    [self addSubview:_tableView];
    //_tableView.backgroundColor = [UIColor blueColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = MAIN_HEIGHT / 4 * 3 / 11;
    [_tableView registerClass:[InformationTableViewCell class] forCellReuseIdentifier:NSStringFromClass([InformationTableViewCell class])];
    _tableView.separatorStyle = 2;
    _tableView.tableHeaderView = _topView;
    _tableView.scrollEnabled = NO;
    //footView
    self.footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT / 7 - 30 + MAIN_WIDTH / 4 * 3 / 11)];
    _footView.backgroundColor = [UIColor colorWithRed:255.0 / 255.0 green:102. / 255.0 blue:0 alpha:1];

    _footView.backgroundColor = LIGHT_GRAY;
    _tableView.tableFooterView = _footView;
    
    self.exitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _exitButton.frame = CGRectMake(0, 0, MAIN_WIDTH / 4 * 3, MAIN_HEIGHT / 7 - 60);
    
    [_footView addSubview:_exitButton];
    _exitButton.center = CGPointMake(_footView.center.x, _footView.frame.size.height / 2);
    [_exitButton setTitle:@"退出登录" forState:UIControlStateNormal];
    _exitButton.backgroundColor = [UIColor orangeColor];
    _exitButton.titleLabel.font = [UIFont systemFontOfSize:20 / 667.0 * MAIN_HEIGHT];
    _exitButton.tintColor = [UIColor whiteColor];
    [_exitButton addTarget:self action:@selector(clickLogout) forControlEvents:UIControlEventTouchUpInside];
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 24, MAIN_WIDTH, 40)];
    [self addSubview:_headView];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    _backButton.frame = CGRectMake(10, 7.5, 25, 25);
    [_headView addSubview:_backButton];
    [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.center.x - 25, 5, 50, 30)];
    _titleLabel.text = @"我的";
    _titleLabel.font = [UIFont systemFontOfSize:20];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_headView addSubview:_titleLabel];
    
    
    self.headLine = [[UIView alloc] initWithFrame:CGRectMake(0, 39, MAIN_WIDTH, 1)];
    [_headView addSubview:_headLine];
    _headLine.backgroundColor = LINE_COLOR;
    
}
- (void)backButtonAction:(UIButton *)button {
    [self.delegate backAction];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InformationTableViewCell class])];
    if (indexPath.row == 3 || indexPath.row == 6) {
        cell.isBooL = 1;
    }
    else {
        cell.isBooL = 0;
    }
    if (indexPath.row < 2) {
        cell.detailLabel.text = _setArray[indexPath.row];
    }
    cell.titleLabel.text = _titleArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3 || indexPath.row == 6) {
        return MAIN_HEIGHT / 4 * 3 / 11 / 2;
    }
    else {
        return MAIN_HEIGHT / 4 * 3 / 11;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.row == 4) {
        [self.delegate passwordAction];
    }else if (indexPath.row == 5) {
        [self.delegate deleteDataAction];
    }else if (indexPath.row == 8){
        [self.delegate sendToAboutUs];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        
        [_setArray replaceObjectAtIndex:0 withObject:_text.text];
        [_tableView reloadData];
    }
}

- (void)clickLogout {
    
    [self.delegate logout];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"headClear" object:nil];
}


- (void)clickHeadImageView {
    
    [self.delegate clickHeaderImage];
    
    
    
    
}

- (void)getToBmob {
    
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        //进行操作
        
        //        self.setArray = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"用户%@", bUser.username], bUser.mobilePhoneNumber, nil];
        self.setArray = [NSMutableArray array];
        [self.setArray addObject:bUser.username];
        
        
        
        if (bUser.mobilePhoneNumber != nil) {
            [self.setArray addObject:bUser.mobilePhoneNumber];
        }else {
            [self.setArray addObject:@""];
        }
        if (bUser.email != nil) {
            [self.setArray addObject:bUser.email];
        }else {
            [self.setArray addObject:@""];
        }
        
        
        
        
        
    }else{
        //对象为空时，可打开用户注册界面
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
